<?php
/*
采购入库单列表
*/
declare(strict_types=1);

namespace Modules\Purchasing\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Purchasing\Models\Purchasewarehousing;
use Modules\Purchasing\Models\PurchasewarehousingAtached;
use Illuminate\Http\Request;
use Modules\Purchasing\Models\Barcodeprinting;
use Modules\Purchasing\Models\Supplier;
use Modules\Purchasing\Models\Supplierschedule;
use Illuminate\Support\Facades\DB;
use Modules\Purchasing\Models\Provisional;
use Modules\Purchasing\Models\Provisionalschedule;
use Catch\Exceptions\FailedException;
use Modules\Purchasing\Models\Purchaseorder;
use Modules\Purchasing\Models\Purchaseorderschedule;
use Modules\Purchasing\Models\PurchasewarehousingGiveaway;
use Modules\Warehouse\Models\Storagelocation;
use Modules\Purchasing\Models\Inspectionlist;
use Modules\Purchasing\Models\Inspectionlistcheck;
use extend\services\warehouse\{warehouseService};//引入自定义类
use Modules\Purchasing\Http\Repositories\PurchasewarehousReget;
use App\Services\RedisLockService;


class PurchasewarehousController extends Controller
{
    public function __construct(
        protected readonly Purchasewarehousing $model
    ){}


    /**
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request): mixed
    {
        // $data = $this->model->getList();
        $data =  PurchasewarehousReget::list($request->all());
        
        $kingDeeService = new warehouseService();
        $datauser = $kingDeeService->uerdata();
        foreach($data as $k=>$v){


            if($v['incomingtype']==0){
                //采购单号
                $purchasewarehousing_id = provisionalschedule::select('purchasewarehousing_id','purchaseorder_id')->where('purchasewarehousing_id',$v['id'])
                ->groupby(['purchasewarehousing_id','purchaseorder_id'])->get()->toArray();
                
                foreach($purchasewarehousing_id as $ks=>$vs){
                    $fbillno = Purchaseorder::select('fbillno','fsupplierid_name','fsupplierid_number')->where('id',$vs['purchaseorder_id'])->first();
                    $fbillno_data = '';
                    $fsupplierid_name = '';
                    $fsupplierid_number = '';
                    if(!empty($fbillno)){
                        $fbillno_data .= trim($fbillno['fbillno']).'、';
                        // $fsupplierid_name .= trim($fbillno['fsupplierid_name']).'、';
                        // $fsupplierid_number .= trim($fbillno['fsupplierid_number']).'、';
                    }
                }
                

                $fbillno_data = rtrim($fbillno_data,'、');
                $fbillno_data = explode('、',$fbillno_data);
                $fbillno_data = array_unique($fbillno_data);
                $fbillnodata='';
                foreach ($fbillno_data as $k1=>$v1){
                    $res = mb_convert_encoding($v1, 'UTF-8', 'UTF-8'); 
                    $fbillnodata .=$res .'、';
                }
                $data[$k]['fbillno'] = rtrim($fbillnodata,'、');

                // $fsupplierid_name = rtrim($fsupplierid_name,'、');
                // $fsupplierid_name = explode('、',$fsupplierid_name);
                // $fsupplierid_name = array_unique($fsupplierid_name);
                // $data[$k]['fsupplierid_name'] = implode('、',$fsupplierid_name); 

                // $fsupplierid_number = rtrim($fsupplierid_number,'、');
                // $fsupplierid_number = explode('、',$fsupplierid_number);
                // $fsupplierid_number = array_unique($fsupplierid_number);
                // $data[$k]['fsupplierid_number'] = implode('、',$fsupplierid_number); 
            }
          

            
            // $data[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]['username']:'';
            $data[$k]['modifier'] = isset($datauser[$v['modifier']])?$datauser[$v['modifier']]['username']:'';
            $data[$k]['state'] = $v['state']==1?'已审核':'未审核'; 
            $data[$k]['writestate'] = $v['writestate']==1?'回写成功':''; 
            $data[$k]['incomingtype'] = $v['incomingtype']==1?'组织间入库':'组织外入库'; 
            // if($v['writestate']==0){
            //     $data[$k]['writestate'] = '';
            // }else if ($v['writestate']==1) {
            //     $data[$k]['writestate'] = '回写成功';
            // }
        }
        return $data;
        // return $this->model->getList();
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {

        $data = $request->all();
        $key = 'CG_' . $data['detailscheck_all'][0]['provisional_id'];
        $lock = new RedisLockService($key,30);
        if(!$lock->lock()){
            throw new \Exception('当前入库处理中，请稍后再试');
        }
        try{
            $data['creator_id'] = $this->getLoginUserId();//当前登录用户名称
            return PurchasewarehousReget::create($data);

        }catch (\Exception $e){
            throw $e;
        } finally {
            $lock->unlock();
        }

        
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id,Request $request)
    {
     
            // try {
                $data = $request->all();
                $Purchasewarehousing = $this->model->select('*')->where('id',$id)->first()->toArray();
                if(empty($Purchasewarehousing)) throw new FailedException('参数出错');
                $datares = PurchasewarehousReget::showdata($Purchasewarehousing,$data);

                if($Purchasewarehousing['state'] == 1){
                    $Purchasewarehousing['state'] = '审核成功';
                }else{
                    $Purchasewarehousing['state'] = '未审核';
                }
                
                if($Purchasewarehousing['writestate'] == 1){
                     $Purchasewarehousing['writestate'] = '回传成功';
                }else{
                    $Purchasewarehousing['writestate'] = '未回传';
                }

                $Purchasewarehousing['detailscheck_all'] = $datares;
                return  ['code'=>1000,'message'=>'请求成功','data'=>$Purchasewarehousing];


            // } catch (\Exception $e) {
            //     DB::rollBack();
            //     throw new FailedException('服务器飞出地球了请联系技术找回');
            // }
    
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function updatedata($id, Request $request)
    {
      
      
       $datares =  $this->model->where('id',$id)->where('state',1)->first();
        if(!empty($datares)){
            throw new FailedException('数据已审核无法修改');
        }
        $data = $request->all();
        $modifier =$this->getLoginUserId();//当前登录用户名称
        return PurchasewarehousReget::createUpdate($data,$modifier);

    }

     /**汇总赠品修改
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function summaryupdatedata($id, Request $request)
    {

        

    //   $purchasewarehousing_id =  Provisionalschedule::select('id','purchasewarehousing_id')->where('id',$id)->first();
    //     if(empty($purchasewarehousing_id)){
    //         throw new FailedException('数据出错');
    //     }
       $data =  $this->model->where('id',$id)->where('state',1)->first();

        if(!empty($data)){
            throw new FailedException('数据已审核无法修改');
        }
        $data = $request->all();
        $modifier =$this->getLoginUserId();//当前登录用户名称
        return PurchasewarehousReget::createUpdatesummary($data,$modifier);

    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return PurchasewarehousReget::delete($id);
    }


    /*
    *采购入库PDA扫描接口
    * @param $id
    * @return mixed
    */
    public function purchaseininbound($code)
    {
        return PurchasewarehousReget::checkPdaApi($code);
    }

    /*
    * 采购入库审核接口
    * @param $id   
    * @return mixed
    */
    public function purchaseexamine($id)
    {
      
        return PurchasewarehousReget::auditupdate($id);
    }


    
    /*
    * 组织间的采购订单数据录入
    * @param $id   
    * @return mixed
    */
    public function purchasepush(Request $request)
    {
        $data = $request->all();
        $data['creator_id'] =$this->getLoginUserId();//当前登录用户名称
        return PurchasewarehousReget::pushdataAadd($data);
    }


    /*PDA 扫描接口
    * 组织间的采购订单数据录入
    * @param $id   
    * @return mixed
    */
    public function purchasepushcode($code)
    {
       
        return PurchasewarehousReget::pushdatacode($code);
    }

    /*PDA 扫描接口 数据提交推送到金蝶
    * 组织间的采购订单数据录入
    * @param $id   
    * @return mixed
    */
    public function purchasepushupdate(Request $request)
    {

        return PurchasewarehousReget::pushdatacodeupdate($request->all());
    }


    /*数据处理 
    */
    public function processing(Request $request)
    {

        return PurchasewarehousReget::processing($request->all());
    }



}
